var closeAllTheTabs = {};

closeAllTheTabs.getUnpinnedTabsOf = function _getUnpinnedTabsOf() {
	var tabs = gBrowser.tabs,
	ret = [];

	for (var i = 0; i < tabs.length ; ++i) {
		var t = tabs[i];
		if (t.pinned) {
			continue;
		}
		ret.unshift(t);
	}

	return ret;
};

closeAllTheTabs.removeAllTheTabs = function _removeAllTheTabs() {
	var aTabs = closeAllTheTabs.getUnpinnedTabsOf();

	if (gBrowser.warnAboutClosingTabs(true)) {
		for (var i = aTabs.length - 1; i >= 0; --i) {
			gBrowser.removeTab(aTabs[i], {animate:true});
		}
	}
};

//Set menuitem's state
window.addEventListener("load", function() {
	document.getElementById('tabContextMenu').addEventListener("popupshowing", function(ev) {
		//Must be called after TabContextMenu.updateContextMenu(menupopup)
		if (ev.target !== this) {
			return;
		}

		var aTabs = closeAllTheTabs.getUnpinnedTabsOf();
		if (aTabs.length == 1 && gBrowser.getBrowserForTab(aTabs[0]).contentDocument.location == "about:blank") {
			document.getElementById("context_closeAllTheTabs").disabled = true;
		} else {
			document.getElementById("context_closeAllTheTabs").disabled = false;
		}
	}, false);
}, false);

